package com.tougutips.modules.user.followers.service.impl;

import com.tougutips.modules.user.followers.entity.Followers;
import com.tougutips.modules.user.followers.entity.User;
import com.tougutips.modules.user.followers.service.RedisFollowService;
import com.tougutips.modules.user.followers.service.UserService;
import com.tougutips.modules.user.followers.util.FollowStatusEnum;
import com.tougutips.modules.user.followers.util.RedisFollowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author:HuangYiF
 * @create: 2022-07-25 15:28
 * @Description:
 */
@Service
public class RedisFollowServiceImpl implements RedisFollowService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserService userService;

    @Override
    public int followOrUnFollow2Redis(Long userId, Long otherUserId) {
        if(otherUserId == null || userId == null){
            return -1;
        }
        int isFollow = 0;
        String followKey = RedisFollowUtil.toFollowKey(userId,otherUserId);
        //如果redis中不存在这个key，则表示是关注动作，否则为取关
        if(redisTemplate.opsForHash().get(RedisFollowUtil.MAP_KEY_FOLLOW_INFO,followKey) == null){
            redisTemplate.opsForHash().put(RedisFollowUtil.MAP_KEY_FOLLOW_INFO,followKey, FollowStatusEnum.FOLLOW.getCode());
            isFollow = 1;
        }
        else{   
            redisTemplate.opsForHash().delete(RedisFollowUtil.MAP_KEY_FOLLOW_INFO,followKey);
        }
        return isFollow;
    }

    @Override
    public List<User> getFollowersFromRedis(Long userId,String type) {
        List<User> userList = new ArrayList<>();
        Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan(RedisFollowUtil.MAP_KEY_FOLLOW_INFO, ScanOptions.NONE);
        while (cursor.hasNext()){
            Map.Entry<Object,Object> entry = cursor.next();
            String key = (String) entry.getKey();
            String[] split = key.split("::");
            Long myId = Long.valueOf(split[0]);
            Long otherId = Long.valueOf(split[1]);
            if("followersList".equals(type)){
                if(myId.equals(userId)){
                    userList.add(userService.getById(otherId));
                }
            }else if("fansList".equals(type)){
                if(otherId.equals(userId)){
                    userList.add(userService.getById(myId));
                }
            }
        }
        return userList;
    }

    @Override
    public List<Followers> getFollowDataFromRedis() {
        List<Followers> followersList = new ArrayList<>();
        Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan(RedisFollowUtil.MAP_KEY_FOLLOW_INFO, ScanOptions.NONE);
        while (cursor.hasNext()){
            Map.Entry<Object,Object> entry = cursor.next();
            String key = (String) entry.getKey();
            String[] split = key.split("::");
            Long userId = Long.valueOf(split[0]);
            Long otherId = Long.valueOf(split[1]);
            Integer status = (Integer) entry.getValue();
            Followers followers = new Followers(userId,otherId,status);
            followersList.add(followers);
            //拿到后删除
            redisTemplate.opsForHash().delete(RedisFollowUtil.MAP_KEY_FOLLOW_INFO,key);
        }
        return followersList;
    }

}
